﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EDiamondModel;

public partial class Pages_BaiViet : System.Web.UI.Page
{
    EDiamondEntities1 db = new EDiamondEntities1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            double sl = GioHang.TongSanpham();
            double tg = GioHang.TongTrigia();
            Label SL = this.Page.Master.FindControl("SLuong") as Label;
            SL.Text = "<b>" + sl.ToString() + "<b>";
            Label TGia = this.Page.Master.FindControl("TGia") as Label;
            TGia.Text = "<b>" + tg.ToString() + "<b>";
            string IDBaiViet = Request.QueryString["IDBaiViet"];
            int? tloai = 0;
            int id = Convert.ToInt32(IDBaiViet);
            if (!string.IsNullOrEmpty(IDBaiViet))
            {

                BaiViet bv = db.BaiViets.FirstOrDefault(x => x.BaiVietID == id);

                lblTuaBaiViet.Text = bv.TuaBaiViet;
                LblNoiDung.Text = bv.NoiDung;
                tloai = bv.TheLoai;
            }

            var query = db.BaiViets.Where(x => x.TheLoai == tloai && x.BaiVietID != id).OrderByDescending(y => y.NgayCapNhat);
            dsBaiViet.DataTextField = "TuaBaiViet";
            dsBaiViet.DataValueField = "BaiVietID";
            dsBaiViet.DataSource = query;
            dsBaiViet.DataBind();

            lblMuctin.Text = tloai == 2 ? "Tin tức liên quan" : (tloai == 3 ? "Cẩm nang kim cương" : "Sự kiện");
            int? tl1 = 0, tl2 = 0;
            switch (tloai)
            {
                case 2:
                    tl1 = 3; tl2 = 4;
                    tab1.InnerText = "Cẩm nang kim cương"; tab2.InnerText = "Sự kiện";
                    break;
                case 3:
                    tl1 = 2; tl2 = 4;
                    tab1.InnerText = "Tin tức"; tab2.InnerText = "Sự kiện";
                    break;
                case 4:
                    tl1 = 3; tl2 = 2;
                    tab1.InnerText = "Cẩm nang kim cương"; tab2.InnerText = "Tin tức";
                    break;
            }

            var temp1 = db.BaiViets.Where(x => x.TheLoai == tl1 && x.BaiVietID != id).OrderByDescending(y => y.NgayCapNhat);
            ds1.DataTextField = "TuaBaiViet";
            ds1.DataValueField = "BaiVietID";
            ds1.DataSource = temp1;
            ds1.DataBind();

            var temp2 = db.BaiViets.Where(x => x.TheLoai == tl2 && x.BaiVietID != id).OrderByDescending(y => y.NgayCapNhat);
            ds2.DataTextField = "TuaBaiViet";
            ds2.DataValueField = "BaiVietID";
            ds2.DataSource = temp2;
            ds2.DataBind();

        }
    }
    private void ChiTietBaiViet(int id)
    {
        BaiViet bv = db.BaiViets.FirstOrDefault(x => x.BaiVietID == id);
        lblTuaBaiViet.Text = bv.TuaBaiViet;
        LblNoiDung.Text = bv.NoiDung;
        int? tloai = 0;
        tloai = bv.TheLoai;

        var query = db.BaiViets.Where(x => x.TheLoai == tloai && x.BaiVietID != id).OrderByDescending(y => y.NgayCapNhat);
        dsBaiViet.DataTextField = "TuaBaiViet";
        dsBaiViet.DataValueField = "BaiVietID";
        dsBaiViet.DataSource = query;
        dsBaiViet.DataBind();

    }
    protected void dsBaiViet_Click(object sender, BulletedListEventArgs e)
    {
        ListItem list = dsBaiViet.Items[e.Index];
        int id = Convert.ToInt32(list.Value);
        ChiTietBaiViet(id);
    }
    protected void ds1_DataBound(object sender, EventArgs e)
    {
        for (int i = 0; i < ds1.Items.Count; i++)
        {
            ds1.Items[i].Value = "~/Pages/BaiViet.aspx?IDBaiViet=" + ds1.Items[i].Value;
        }
    }
    protected void ds2_DataBound(object sender, EventArgs e)
    {
        for (int i = 0; i < ds2.Items.Count; i++)
        {
            ds2.Items[i].Value = "~/Pages/BaiViet.aspx?IDBaiViet=" + ds2.Items[i].Value;
        }
    }
}